package models;

import java.util.ArrayList;
import java.util.List;
import siena.*;
import siena.ClassInfo;

public class InternshipFamiliar extends Model {
	
	   @Id(Generator.AUTO_INCREMENT)
	   public Long id;
	   @NotNull
	   public User internship;
	   @NotNull
	   public User familiar;

	
	   public InternshipFamiliar(User i, User f) {
			internship = i;
			familiar = f;
	   }
	   
	   static Query<InternshipFamiliar> all() {
			return Model.all(InternshipFamiliar.class);
	   }
	   
	   public static Query<InternshipFamiliar> findAll() {
			return all();
		}
	   
	   public static List<InternshipFamiliar> getAll()  {
			return findAll().fetch();
		}
	   
	   
	   public static int countByFamiliar(User familiar) {
			return findByFamiliar(familiar).count();
	   }
	   
	   public static Query<InternshipFamiliar> findByFamiliar(User familiar) {
			return all().filter("familiar", familiar);
	   }
	   
	   public static List<User> getByFamiliar(User familiar) {
			List<InternshipFamiliar> internshipFamiliars =  all().filter("familiar", familiar).fetch();
			List<User> internships = new ArrayList<User>();
			for(InternshipFamiliar internshipFamiliar : internshipFamiliars) {
				internships.add(User.findById(internshipFamiliar.internship.id));
			}
			return internships;
	  }
	   
	   
	   public static Query<InternshipFamiliar> findByInternship(User internship) {
			return  all().filter("internship", internship);
	   }
	   
	   public static int countByInternship(User internship) {
			return findByInternship(internship).count();
	   }
	   
	   public static List<User> getByInternship(User internship) {
			List<InternshipFamiliar> internshipFamiliars =  findByInternship(internship).fetch();
			List<User> familiars = new ArrayList<User>();
			for(InternshipFamiliar internshipFamiliar : internshipFamiliars) {
				familiars.add(User.findById(internshipFamiliar.familiar.id));
			}
			return familiars;
	   }
	   
	   public static List<User> paginateByInternship(Query<InternshipFamiliar> query, int offset, int limit) {
		   List<InternshipFamiliar> internshipFamiliars = query.fetch(limit, offset);
		   List<User> internships = new ArrayList<User>();
		   for(InternshipFamiliar internshipFamiliar : internshipFamiliars) {
			   internships.add(User.findById(internshipFamiliar.familiar.id));
		   }
		   return internships;
	   }
	   
	   public static List<User> paginateByFamiliar(Query<InternshipFamiliar> query, int offset, int limit) {
		   List<InternshipFamiliar> internshipFamiliars = query.fetch(limit, offset);
		   List<User> internships = new ArrayList<User>();
		   for(InternshipFamiliar internshipFamiliar : internshipFamiliars) {
			   internships.add(User.findById(internshipFamiliar.internship.id));
		   }
		   return internships;
	   }
	   
	   public static void delete(User user) {
		   all().filter("internship", user).delete();
		   all().filter("familiar", user).delete();
	   }

		
}